class LoggedControllersController < ApplicationController
  caches_page :index, :show, :graphs
  caches_action :graph_of_completion_times, :graph_of_db_times, 
    :graph_of_hits, :graph_of_render_times
	before_filter :get_logged_controllers, :except => [:show, :show_in_table]

	# GET /logged_controllers
  # GET /logged_controllers.xml
  def index
  	@dir = params[:dir] || "normal"
  	@sortby = params[:sortby] || "name"
  	
    @logged_controllers = case @sortby
      when "name" 	: @logged_controllers.sort_by { |lc| lc.name }
      when "count"	: @logged_controllers.sort { |lca, lcb| lcb.count <=> lca.count }
      when "completion_time"	: @logged_controllers.sort { |lca, lcb| 
          lcb.average_completion_time <=> lca.average_completion_time }
      when "render_time"	: @logged_controllers.sort { |lca, lcb| lcb.average_render_time <=> lca.average_render_time }
      when "db_time"	: @logged_controllers.sort { |lca, lcb| lcb.average_db_time <=> lca.average_db_time }
      else @logged_controllers
    end
    @logged_controllers.reverse! unless @dir == "normal"
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @logged_controllers }
    end
  end
  
  def show
	  @dir = params[:dir] || "normal"
  	@sortby = params[:sortby] || "name"
  	@logged_controller = LoggedController.find params[:id]
  	respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @logged_controller }
      format.js   # show.rjs
    end
  end
  
  # used for XHR; seperated from show because caching will block this from working right
  def show_in_table
    @dir = params[:dir] || "normal"
    @sortby = params[:sortby] || "name"
    @logged_controller = LoggedController.find params[:id]
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @logged_controller }
      format.js   # show.rjs
    end
  end
  
  def graphs
  	@graph_of_hits = open_flash_chart_object(500, 500, graph_of_hits_logged_controllers_url)
  	@graph_of_completion_times = open_flash_chart_object(900, 300, graph_of_completion_times_logged_controllers_url)
  	@graph_of_render_times = open_flash_chart_object(900, 300, graph_of_render_times_logged_controllers_url)
  	@graph_of_db_times = open_flash_chart_object(900, 300, graph_of_db_times_logged_controllers_url)
  end

  def graph_of_hits
    ofc = ofc_pie_graph_with_defaults(@collection_for_graph, 'count', 'title_name', 
      "Percentage of Hits per Controller", @collection_for_graph.map { |mem| "/logged_controllers/#{mem.id}" })
  	render :text => ofc.render
  end
  
  def graph_of_completion_times
	  ofc = ofc_bar_graph_with_defaults @logged_controllers, 'average_completion_time_in_ms', 'title_name', 
	  				'Completion Time per Controller',	'Average Time to Complete (in milliseconds) Controller' 
    render :text => ofc.render
	end
  
  # I tried this out but it doesn't really make sense - I was looking for more of a box chart
  def candle_graph_of_completion_times
    ofc = ofc_candle_graph_with_defaults @logged_controllers, 
      'max_completion_time_in_ms', 'average_completion_time_in_ms', 
       'min_completion_time_in_ms', 
      'title_name', 'Completion Time per Controller',	'Average Time to Complete (in milliseconds) Controller' 
    render :text => ofc.render
  end
  
  def graph_of_render_times
	  ofc = ofc_bar_graph_with_defaults @logged_controllers, 'average_render_time_in_ms', 'title_name', 
	  				'Render Time per Controller', 'AverageTime to Render (in milliseconds) by Controller' 
    render :text => ofc.render
  end
  
  def graph_of_db_times
	  ofc = ofc_bar_graph_with_defaults @logged_controllers, 'average_db_time_in_ms', 'title_name', 
            'DB Access Time per Controller', 'Average DB Access Time (in milliseconds) by Controller' 
    render :text => ofc.render
  end
  
  
  private
  
  def get_logged_controllers
	  @logged_controllers = LoggedController.find(:all, :order => "name ASC")
    sum_of_hits = @logged_controllers.map(&:count).compact.sum.to_f
    @collection_for_graph, @other_controllers = @logged_controllers.partition { |c| (c.count / sum_of_hits) > 0.01 }
    if @other_controllers.size > 1
      @collection_for_graph << LoggedController.new( :count => @other_controllers.map(&:count).sum, 
        :name => "Other")
    elsif @other_controllers.size == 1
      @collection_for_graph << @other_controllers.first
    end
	end
  
end
